*Dataset with singles*

*Load data*
clear

use "Data\sample1_new.dta"

*Keep only relevant variables
keep pnr aar wage_growth_ambition final_educ wage_start_mean_ambition grad_region educ_eika fined civst faelle_nr koen aegte_nr individual erhvervsindk_13 alder job_time_loen_smal year_effects


forvalues i=81(1)85{
	
*9th grade
gen temp_g=wage_growth_ambition if final_educ==1109`i'
egen temp_g_2=max(temp_g)
replace wage_growth_ambition=temp_g_2 if (final_educ==1007 | final_educ==1008 | final_educ==1023 | final_educ==1123 | final_educ==1009 | final_educ==1022) & grad_region==`i' 
drop temp_g temp_g_2

gen temp_w=wage_start_mean_ambition if final_educ==1109`i'
egen temp_w_2=max(temp_w)
replace wage_start_mean_ambition=temp_w_2 if (final_educ==1007 | final_educ==1008 | final_educ==1023 | final_educ==1123 | final_educ==1009 | final_educ==1022) & grad_region==`i' 
drop temp_w temp_w_2

replace final_educ=1107`i' if final_educ==1107 & grad_region==`i'
replace final_educ=1008`i' if final_educ==1008 & grad_region==`i'
replace final_educ=1023`i' if final_educ==1023 & grad_region==`i'
replace final_educ=1123`i' if final_educ==1123 & grad_region==`i'
replace final_educ=1009`i' if final_educ==1009 & grad_region==`i'
replace final_educ=1022`i' if final_educ==1022 & grad_region==`i'

*10th grade
gen temp_g=wage_growth_ambition if final_educ==1110`i'
egen temp_g_2=max(temp_g)
replace wage_growth_ambition=temp_g_2 if final_educ==1010 & grad_region==`i' 
drop temp_g temp_g_2

gen temp_w=wage_start_mean_ambition if final_educ==1110`i'
egen temp_w_2=max(temp_w)
replace wage_start_mean_ambition=temp_w_2 if final_educ==1010 & grad_region==`i' 
drop temp_w temp_w_2

replace final_educ=1010`i' if final_educ==1010 & grad_region==`i'

}

*3.g 
gen temp_g=wage_growth_ambition if final_educ==1198
egen temp_g_2=max(temp_g)
replace wage_growth_ambition=temp_g_2 if final_educ==1097
drop temp_g temp_g_2

gen temp_w=wage_start_mean_ambition if final_educ==1198
egen temp_w_2=max(temp_w)
replace wage_start_mean_ambition=temp_w_2 if final_educ==1097
drop temp_w temp_w_2


***ADD ON MANGER***

merge 1:1 pnr aar using "Data\manager_training.dta", keepusing(ever_top_manager disco top_manager)
drop if _merge==2
drop _merge

sort pnr aar
by pnr: gen temp=_n
gen temp2=temp if top_manager==1
by pnr: egen temp3=min(temp2)
gen promotion=1 if temp3==temp
drop temp*

*Avr age at promotion by program
destring alder, replace
gen temp=alder if promotion==1

sort final_educ
by final_educ: egen avr_promo_age=mean(temp)
drop temp

gen log_hourly_wage=log(job_time_loen_smal)-year_effects

*Manager premium by program

gen temp=log_hourly_wage if alder>=avr_promo_age & top_manager==0
gen temp2=log_hourly_wage if alder>=avr_promo_age & top_manager==1

gen temp_count=1 if !missing(temp)
by final_educ: egen non_man_count=count(temp_count)
gen temp_count2=1 if !missing(temp2)
by final_educ: egen man_count=count(temp_count2)

gen temp_wage=temp if non_man_count>=10 & man_count>=10
by final_educ: egen non_man_wage=mean(temp_wage)

gen temp_wage2=temp2 if non_man_count>=10 & man_count>=10
by final_educ: egen man_wage=mean(temp_wage2)

drop temp*

gen temp=man_wage-non_man_wage
gen inflex_manager=exp(temp)
drop temp
replace inflex_manager=. if final_educ==. | final_educ==1

***


**kmeans**

*standardize variables
sum wage_start_mean_ambition
sca the_mean_s=r(mean)
sca the_sd_s=r(sd)
gen wage_start_mean_ambition_s=(wage_start_mean_ambition-the_mean_s)/the_sd_s
sum wage_start_mean_ambition_s

sum wage_growth_ambition
sca the_mean_g=r(mean)
sca the_sd_g=r(sd)
gen wage_growth_ambition_s=(wage_growth_ambition-the_mean_g)/the_sd_g
sum wage_growth_ambition_s


sum inflex_manager
sca the_mean_g=r(mean)
sca the_sd_g=r(sd)
gen inflex_manager_s=(inflex_manager-the_mean_g)/the_sd_g
sum inflex_manager_s

*Graph
by final_educ: gen n=_n
by final_educ: egen educ_count=count(final_educ)

kdensity inflex_manager if n==1 & educ_count>=10
kdensity inflex_manager_s if n==1 & educ_count>=10

drop n educ_count

*

*Ver 1

cluster kmeans wage_start_mean_ambition_s inflex_manager_s, k(4) name(ambition_type_w0_inflex_man) s(kr(1234))
tab ambition_type_w0_inflex_man
tabstat wage_start_mean_ambition_s inflex_manager_s, by(ambition_type_w0_inflex_man)

tab ambition_type_w0_inflex_man fined, row






*Define marital status
gen relationship=0
replace relationship=1 if civst=="G" | (civst!="G" & faelle_nr!="")

gen married=0
replace married=1 if civst=="G"

gen cohab=0
replace cohab=1 if relationship==1 & married==0

*Make couple ID based on the PNR of the man (maybe check later if we get the same by using PNR of the woman)
gen couple_id="."
replace couple_id=pnr if koen=="1" & relationship==1
replace couple_id=aegte_nr if koen=="2" & married==1
replace couple_id=faelle_nr if koen=="2" & cohab==1
sort couple_id aar

*Keep only couples where we observe both partners
gen temp2=koen if relationship==1
destring temp2, replace
by couple_id aar: egen temp3=mean(temp2)
keep if (temp3>1 & temp3<2 & relationship==1) | relationship==0
drop temp2 temp3


sort pnr aar

**Keep only those for who we observe ambition type**
keep if wage_growth_ambition!=.

*Keep only couples where we observe both partners
gen temp2=koen if relationship==1
destring temp2, replace
by couple_id aar, sort: egen temp3=mean(temp2)
keep if (temp3==1.5 & relationship==1) | relationship==0
drop temp2 temp3

sort pnr aar

*Only keep those with non missing robustness check ambition types*

keep if ambition_type_w0_inflex_man!=.

*Keep only couples where we observe both partners
gen temp2=koen if relationship==1
destring temp2, replace
by couple_id aar, sort: egen temp3=mean(temp2)
keep if (temp3==1.5 & relationship==1) | relationship==0
drop temp2 temp3

sort pnr aar


*Save*
save "Results\tab_B3\Scenario_12\dataset_with_singles_inflex_manager.dta", replace

